<?php

require_once("common.inc.php");
require_once("db.inc.php");
require_once("queries.inc.php");

auth();
$dbh = get_db();

initSession();

xmlhead("Woorden overhoren");

$languages = getLanguages();

?>
<body>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"] ?>">
<p>

<?php //dropdowns

dropdown("interface_language", $_SESSION['interface_language'], "You speak:", $languages, true);
dropdown("learn_language", $_SESSION['learn_language'], "You are learning:", $languages, true);

if ($_SESSION['interface_language'] > 0 && $_SESSION['learn_language'] > 0) {
	$test_modes = array(
		'1' => $languages[$_SESSION['learn_language']] . " -> " . $languages[$_SESSION['interface_language']],
		'2' => $languages[$_SESSION['interface_language']] . " -> " . $languages[$_SESSION['learn_language']]
	);
	dropdown("test_mode", $_SESSION['test_mode'], "", $test_modes, true);
}

?>

<!-- &#336; &#337; &#368; &#369; -->
<input type="submit" value="Go!" />
</p>
</form>
<?php

if ($_SESSION['interface_language'] == $_SESSION['learn_language']) echo "<p>Oooh, what a challenge...</p>\n"; 

if ($_SESSION['learn_language'] > 0) {

?>
<form method="post" action="wordtest_grade.php">
<p>
<input type="hidden" name="interface_language" value="<?php echo $_SESSION['interface_language'] ?>" />
<input type="hidden" name="learn_language" value="<?php echo $_SESSION['learn_language'] ?>" />
</p>
<?php


	if ($_SESSION['test_mode'] == 1) { //normal testing: get max. 10 words in the learn_language, and ask for a translation into a language other than learn_language. Simply put: do you know what this word means?
		
		?>
<input type="hidden" name="test_mode" value="1">

<?php
		
		//this can possibly be optimized, because no data is needed from uw_defined_meaning, other than remove_transaction_id. On the other hand, keep it in until we can sanitize wordgroups.
		$query = mysql_query(
			"SELECT ws.syntrans_sid, spelling, shelf, lastguess
			FROM
				uw_syntrans s,
				uw_expression e,
				uw_defined_meaning dm,
				wvt_wordgroup_subscriptions wsub,
				wvt_wordgroup_syntranses ws
			LEFT JOIN
				wvt_wordguesses wg
			ON wg.syntrans_sid = ws.syntrans_sid AND wg.test_mode = 1
			
			WHERE
			wsub.user_id = {$_SESSION['user_id']}
			AND wsub.enabled = 1
			AND s.syntrans_sid = ws.syntrans_sid
			AND s.remove_transaction_id IS NULL
			AND s.expression_id = e.expression_id
			AND e.remove_transaction_id IS NULL
			AND s.defined_meaning_id = dm.defined_meaning_id
			AND dm.remove_transaction_id IS NULL
			AND ws.wordgroup_id = wsub.wordgroup_id
			AND e.language_id = {$_SESSION['learn_language']}
			AND (
				wg.syntrans_sid IS NULL
				OR
				UNIX_TIMESTAMP() - UNIX_TIMESTAMP(wg.lastguess) > {$_SESSION['sleeptime']}
			)
			ORDER BY wg.shelf ASC, RAND()
			LIMIT {$_SESSION['test_size']}
			",$dbh
		) or die ("can't select syntranses for test: " . mysql_error());
		if (mysql_num_rows($query) == 0) {
			echo "<p>no words could be selected!</p>";
		} else {
			$i = 1;
			while ($row = mysql_fetch_array($query)) {
				if (!is_numeric($row['shelf'])) $row['shelf'] = -1;
?>
<p class="<?php if ($i % 2) echo "odd"; else echo "even"; ?>">
<label for="a<?php echo $i ?>">
<?php
				echo $row['spelling'];
				if ($_SESSION['shelfdebug']) echo " ({$row['shelf']}) ";
?>
</label>
<input id="a<?php echo $row['syntrans_sid'] ?>" name="a<?php echo $row['syntrans_sid'] ?>" autocomplete="off" size="64" /> <!-- TODO: put size into CSS -->
<input type="hidden" name="id[]" value="<?php echo $row['syntrans_sid'] ?>" />
<?php
				$i++;
				
				//query to say which answers are _not_ desired, i.e. syntranses on a higher shelf than the intended syntrans.
				//should this also take into account words that are not in the subscribed wordgroups? I think so, but these should be clearly distinguished in the output. Maybe also distinguish between words in a non-subscribed wordgroup and words not in any wordgroup.
				//TODO: rewrite for new schema
				
				/*
				$subquerytext = "SELECT word
					FROM dict_words
					WHERE word_id = (
						SELECT w.word_id
						FROM dict_words w, dict_wordguesses wg
						WHERE shelf > {$row['shelf']}
						AND wg.user_id = {$_SESSION['user_id']}
						AND w.word_id = wg.word_id
						AND wg.from_lang_id = {$_SESSION['interface_language']}
						AND wg.to_lang_id = {$_SESSION['learn_language']}
						AND w.lang_id = {$_SESSION['interface_language']}
						AND w.word = '{$row['word']}'
						AND w.word_id != {$row['word_id']}
					)
					AND lang_id = {$_SESSION['learn_language']}";
				$subquery = mysql_query($subquerytext, $dbh) or die (mysql_error());
				while ($subrow = mysql_fetch_array($subquery)) {
					echo " not \"{$subrow['word']}\";";
				}
				*/
				
				?>
	<br />
	</p>

	<?php
			} //end while(one of the <= 10 words)
			?>
<p>
<input type="submit" value="Submit answers" />
</p>
<?php
		} // end if words could be selected
	} else if ($_SESSION['test_mode'] == 2) { //inverse testing: get max. 10 words (with DM) in the interface_language, which have a syntrans in the learn_language in the wordgroups. Simply put: can you express this concept in the language you are learning? 
		?>
<input type="hidden" name="test_mode" value="2">
<?php
		//get maximum [test_size] syntranses which have a translation in the subscribed wordgroups
		//TODO: get rid of test size and just test one at a time. Then we can test whether there is a translation available before selecting it for the test.
		
		$query = mysql_query(
			"SELECT s.defined_meaning_id, ws.syntrans_sid, shelf, lastguess
			FROM
				uw_syntrans s,
				uw_expression e,
				uw_defined_meaning dm,
				wvt_wordgroup_subscriptions wsub,
				wvt_wordgroup_syntranses ws
			LEFT JOIN
				wvt_wordguesses wg
			ON wg.syntrans_sid = ws.syntrans_sid AND wg.test_mode = 2
			
			WHERE
			wsub.user_id = {$_SESSION['user_id']}
			AND wsub.enabled = 1
			AND s.syntrans_sid = ws.syntrans_sid
			AND s.remove_transaction_id IS NULL
			AND s.expression_id = e.expression_id
			AND e.remove_transaction_id IS NULL
			AND s.defined_meaning_id = dm.defined_meaning_id
			AND dm.remove_transaction_id IS NULL
			AND ws.wordgroup_id = wsub.wordgroup_id
			AND e.language_id = {$_SESSION['learn_language']}
			AND (
				wg.syntrans_sid IS NULL
				OR
				UNIX_TIMESTAMP() - UNIX_TIMESTAMP(wg.lastguess) > {$_SESSION['sleeptime']}
			)
			ORDER BY wg.shelf ASC, RAND()
			LIMIT {$_SESSION['test_size']}
			",$dbh
		) or die ("can't select syntranses for test: " . mysql_error());
		
		if (mysql_num_rows($query) == 0) {
			echo "<p>no words could be selected!</p>";
		} else {

			$i = 1;
		
			while ($row = mysql_fetch_array($query)) {
				if (!is_numeric($row['shelf'])) $row['shelf'] = -1;
				$question_expression = get_expression_for_dm($row['defined_meaning_id']);
				$question_word = $question_expression['spelling'];
				$question_text = str_replace("%'%", "\'", get_text_for_dm($row['defined_meaning_id'])); //maybe get this AJAX'ly onMouseOver instead of prefetching?
				?>
<p class="<?php if ($i % 2) echo "odd"; else echo "even"; ?>">
<label for="a<?php echo $i ?>">
<?php
				echo $question_word;
				if ($_SESSION['shelfdebug']) echo " ({$row['shelf']}) ";
				echo "(<a href=\"javascript:alert('$question_text')\">description</a>)";
				?>
</label>
<input id="a<?php echo $row['syntrans_sid'] ?>" name="a<?php echo $row['syntrans_sid'] ?>" autocomplete="off" size="64" /> <!-- TODO: put size into CSS -->
<input type="hidden" name="id[]" value="<?php echo $row['syntrans_sid'] ?>" />
<?php
				$i++;
				//TODO: add "negative" suggestions ("the answer is not: ...")
			} //end while (one of the words)
		?>
<p>
<input type="submit" value="Submit answers" />
</p>
<?php

		}//end if (words could be selected)
		
	} else {
		die ("no other test modes are available");
	}
	?>
</form>
<?php
} // end if $_SESSION['learn_language'] > 0

xmlfoot();
?>